<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot-scope="{}" slot="serviceNoSearch">
        <select-page ref="searchSerSelect" label="serviceName" value="serviceNo" :select.sync="search.serviceNo" :url="website.searchLink.serviceUrl" placeholder-text="服务商" filter-key="searchParam"></select-page>
      </template>
      <template slot-scope="{disabled,size}" slot="beginTimeSearch">
        <el-date-picker v-model="search.beginTime" type="datetime" :editable="false" :clearable="true" value-format='yyyy-MM-dd HH:mm:ss' @change="beginTimeChange" placeholder="请输入 开始日期"></el-date-picker>
      </template>
      <template slot-scope="{disabled,size}" slot="endTimeSearch">
        <el-date-picker v-model="search.endTime" type="datetime" :editable="false" :clearable="true" default-time="23:59:59" value-format='yyyy-MM-dd HH:mm:ss' @change="endTimeChange" placeholder="请输入 结束日期"></el-date-picker>
      </template>
      <template slot="menuLeft">
        <!-- <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.money_change_delete"
          @click="handleDelete"
          >删 除
        </el-button> -->
      </template>
      <template slot="menuRight">
        <!-- <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          v-if="permission.money_change_import"
          @click="handleImport"
          >导入
        </el-button> -->
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.wallet_stream_export"
          @click="handleExport"
          >导出
        </el-button>
      </template>
    </avue-crud>
    <!-- <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog> -->
  </basic-container>
</template>

<script>
import { getList,getExport } from "@/api/rb-oem/moneychange/money_change";
import selectPage from "@/components/select-page/select-page";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";
import {dateFormat} from "@/util/date";
export default {
  components: {
    selectPage
  },
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        delBtn:false,
        editBtn:false,
        addBtn:false,
        selection: true,
        searchMenuSpan: 6,
        menu:false,
        columnBtn: false,
        labelWidth:120,      //弹出表单的标题的统一宽度
        searchLabelWidth: 120, //搜索框标题宽度
        column: [
          {
            label: "服务商编号",
            prop: "serviceNo",
            type: "input",
            sortable: true,
            search: true,
            overHidden: true,
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            type: "input",
            sortable: true,
            search: false,
            overHidden: true,
          },
          {
            label: "余额变动类型",
            prop: "balanceChangeKind",
            type: "select",
            sortable: true,
            search: true,
            dataType: 'number',
            rules: [
              {
                required: true,
                message: "请选择余额变动类型",
                trigger: "blur",
              },
            ],
            dicUrl: "/api/blade-system/dict/dictionary?code=balance_change_kind",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            overHidden: true,
          },
          {
            label: "入账前金额",
            prop: "beforeAmount",
            type: "input",
            sortable: true,
            search: false,
            overHidden: true,
          },
          {
            label: "入账金额",
            prop: "amount",
            type: "input",
            sortable: true,
            search: false,
            overHidden: true,
          },

          {
            label: "入账后金额",
            prop: "afterAmount",
            type: "input",
            sortable: true,
            search: false,
            overHidden: true,
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: "input",
            sortable: true,
            search: false,
            overHidden: true,
          },
          {
            label: "备注",
            prop: "remark",
            type: "input",
            sortable: true,
            search: false,
            overHidden: true,
          },
          {
            label: "开始日期",
            prop: "beginTime",
            search: true,
            hide: true,
            display: false
          },
          {
            label: "结束日期",
            prop: "endTime",
            search: true,
            hide: true,
            display: false
          },
        ],
      },
      data: [],
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-oem/oem/moneychange/money_change/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.money_change_add, false),
        viewBtn: this.vaildData(this.permission.money_change_view, false),
        delBtn: this.vaildData(this.permission.money_change_delete, false),
        editBtn: this.vaildData(this.permission.money_change_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    // rowSave(row, done, loading) {
    //   add(row).then(
    //     () => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //       done();
    //     },
    //     (error) => {
    //       loading();
    //       window.console.log(error);
    //     }
    //   );
    // },
    // rowUpdate(row, index, done, loading) {
    //   update(row).then(
    //     () => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //       done();
    //     },
    //     (error) => {
    //       loading();
    //       window.console.log(error);
    //     }
    //   );
    // },
    // rowDel(row) {
    //   this.$confirm("确定将选择数据删除?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       return remove(row.id);
    //     })
    //     .then(() => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //     });
    // },
    // handleDelete() {
    //   if (this.selectionList.length === 0) {
    //     this.$message.warning("请选择至少一条数据");
    //     return;
    //   }
    //   this.$confirm("确定将选择数据删除?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       return remove(this.ids);
    //     })
    //     .then(() => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //       this.$refs.crud.toggleSelection();
    //     });
    // },
    beforeOpen(done,type) {
      if (["edit", "view"].includes(type)) {
      //   getDetail(this.form.id).then((res) => {
      //     this.form = res.data.data;
      //   });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {

      let params ={
        ...this.search,
        current:'-1',
        size:'-1'
      }

      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //this.forInSearch()
        getExport(params).then((res) => {
             // downloadFileExcel(res);
            this.$message({
              type: "success",
              message: res.data.msg
            });
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    handleTemplate() {
      window.open(
        `/api/rb-oem/oem/moneychange/money_change/export-template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
    beginTimeChange() {
      if(this.search.endTime){
        if(Date.parse(this.search.beginTime) > Date.parse(this.search.endTime)){
          this.$alert('统计开始日期不能大于结束日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          this.search.beginTime = dateFormat(new Date(this.search.endTime),"yyyy-MM-dd")+' 00:00:00';
        }
      }
    },
    endTimeChange() {
      if(this.search.beginTime){
        if(Date.parse(this.search.endTime) < Date.parse(this.search.beginTime)){
          this.$alert('统计结束日期不能小于开始日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          this.search.endTime = dateFormat(new Date(this.search.beginTime),"yyyy-MM-dd")+' 23:59:59';

        }
      }
    },
  },
};
</script>

<style>
</style>
